package com.lijin.batchsys.framework.common;


import java.util.Date;

import com.lijin.batchsys.framework.control.BatchExecContext;
import com.lijin.batchsys.framework.control.StepExecContext;
import com.lijin.batchsys.framework.dao.BatchLogDAO;
import com.lijin.batchsys.framework.dao.model.BatchExecLog;
import com.lijin.batchsys.framework.dao.model.BatchExecLogDetail;
import com.lijin.batchsys.framework.dao.model.BatchInfo;
import com.lijin.batchsys.framework.dao.model.StepInfo;
import com.lijin.batchsys.framework.enums.RunningStatus;

public class DBBatchLogger implements IBatchLogger {
	
	private BatchLogDAO batchLogDAO;
	
	@Override
	public void logBatchContext(BatchExecContext batchCtx, boolean updFlg) {
		// 如果不是更新状态，则向数据库新增批次执行日志
		if (!updFlg) {
			BatchInfo runningBatchInfo = batchCtx.getRunningBatchInfo();
			
			// 初始化批次日志
			BatchExecLog log = new BatchExecLog();
			String logId = UUIDGenerator.getUUID();
			log.setLogId(logId);//日志主键ID
			log.setBatchNo(runningBatchInfo.getBatchNo());
			log.setRunningMode(batchCtx.getRunningMode().getCode());
			log.setBatchDate(batchCtx.getBatchDateStr());
			log.setEndTime("");
			log.setStatus(RunningStatus.RUNNING.getCode());
			log.setExecMsg(batchCtx.getExecMsg());
			batchLogDAO.saveBatchExecLog(log);
			
			// 把批次日志记录从数据库中查出，并放到批次上下文中
			BatchExecLog dbLog = batchLogDAO.queryBatchExecLogById(logId);
			batchCtx.setBatchExecLog(dbLog);
		} else {
			BatchExecLog batchExecLog = batchCtx.getBatchExecLog();
			AssertUtil.objIsNull(batchExecLog, "批次上下文中无批次执行记录");
			
			// 根据上下文更新批次记录状态和执行信息字段，并更新至数据库
			batchExecLog.setStatus(batchCtx.getStatus().getCode());
			batchExecLog.setExecMsg(batchCtx.getExecMsg() == null ? "" : batchCtx.getExecMsg());
			if (batchCtx.getStatus() == RunningStatus.COMPLETED
				|| batchCtx.getStatus() == RunningStatus.COMPLETED){
				batchExecLog.setEndTime(BatchUtil.formatDateTo17Str(new Date()));
			} else {
				batchExecLog.setEndTime("");
			}
			batchLogDAO.updatebatchExecLog(batchExecLog);
		}
	}

	@Override
	public void logStepContext(StepExecContext stepCtx, boolean updFlg) {
		// 如果不是更新状态，则向数据库新增步骤执行日志
		if (!updFlg) {
			StepInfo runningStepInfo = stepCtx.getStepInfo();
			
			// 初始化步骤执行日志
			BatchExecLogDetail logDetail = new BatchExecLogDetail();
			String detailid = UUIDGenerator.getUUID();
			logDetail.setLogDetailId(detailid);
			logDetail.setLogId(stepCtx.getBatchContext().getBatchExecLog().getLogId());//日志主键ID
			logDetail.setBatchNo(runningStepInfo.getBatchNo());
			logDetail.setJobNo(runningStepInfo.getJobNo());
			logDetail.setStepNo(runningStepInfo.getStepNo());
			logDetail.setBatchDate(stepCtx.getBatchContext().getBatchDateStr());
			logDetail.setEndTime("");
			logDetail.setStatus(RunningStatus.RUNNING.getCode());
			logDetail.setExecMsg("");
			logDetail.setErrMsg("");
			batchLogDAO.saveBatchExecLogDetail(logDetail);
			
			// 把步骤执行日志记录从数据库中查出，并放到步骤执行上下文中
			BatchExecLogDetail dbLogDetail = batchLogDAO.queryBatchExecLogDetailById(detailid);
			stepCtx.setBatchExecLogDetail(dbLogDetail);
		} else {
			BatchExecLogDetail logDetail = stepCtx.getBatchExecLogDetail();
			AssertUtil.objIsNull(logDetail, "步骤上下文中无步骤执行记录");
			
			// 根据上下文更新批次记录状态和执行信息字段，并更新至数据库
			logDetail.setStatus(stepCtx.getStatus().getCode());
			logDetail.setExecMsg(stepCtx.getExecMsg() == null ? "" : stepCtx.getExecMsg());
			logDetail.setErrMsg(stepCtx.getErrMsg() == null ? "" : stepCtx.getErrMsg());
			if (stepCtx.getStatus() == RunningStatus.COMPLETED
				|| stepCtx.getStatus() == RunningStatus.FAILED){
				logDetail.setEndTime(BatchUtil.formatDateTo17Str(new Date()));
			} else {
				logDetail.setEndTime("");
			}
			batchLogDAO.updateBatchExecLogDetail(logDetail);
		}
	}

	public void setBatchLogDAO(BatchLogDAO batchLogDAO) {
		this.batchLogDAO = batchLogDAO;
	}

}
